package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.MeetingAppointmentMapper;
import com.zhijian.medical.entity.po.MeetingAppointment;
import com.zhijian.medical.entity.vo.request.MeetingAppointmentSearchVo;
import com.zhijian.medical.entity.vo.request.MeetingAppointmentSingleVo;
import com.zhijian.medical.entity.vo.response.MeetingAppointmentVo;
import com.zhijian.medical.service.MeetingAppointmentService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 会议室预约记录业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-09-26 10:09:24
 */
@Slf4j
@Service
public class MeetingAppointmentServiceImpl implements MeetingAppointmentService {

    @Autowired
    private MeetingAppointmentMapper mapper;

    @Override
    public MeetingAppointment selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public MeetingAppointmentVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(MeetingAppointment meetingAppointment) {
        Date now = new Date();
        meetingAppointment.setCreateTime(now);
        meetingAppointment.setUpdateTime(now);
        return mapper.insert(meetingAppointment) > 0 ? meetingAppointment.getId() : 0L;
    }

    @Override
    public boolean update(MeetingAppointment meetingAppointment) {
        Date now = new Date();
        meetingAppointment.setUpdateTime(now);
        return mapper.updateByPrimaryKey(meetingAppointment) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(MeetingAppointment record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(MeetingAppointmentSearchVo meetingAppointmentSearchVo) {
        return mapper.count(meetingAppointmentSearchVo);
    }

    @Override
    public List<MeetingAppointmentVo> selectList(MeetingAppointmentSearchVo meetingAppointmentSearchVo) {
        return mapper.selectList(meetingAppointmentSearchVo);
    }

    @Override
    public Page<MeetingAppointmentVo> selectPage(MeetingAppointmentSearchVo meetingAppointmentSearchVo) {
        //先直接查总数
        int count = count(meetingAppointmentSearchVo);
        //分页
        Page<MeetingAppointmentVo> page = new Page<>(true, count, meetingAppointmentSearchVo.getPageNum(), meetingAppointmentSearchVo.getPageSize());
        meetingAppointmentSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<MeetingAppointmentVo> responseVos = this.selectList(meetingAppointmentSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<MeetingAppointment> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public MeetingAppointmentVo single(MeetingAppointmentSingleVo meetingAppointmentSingleVo) {
        return mapper.single(meetingAppointmentSingleVo);
    }

}